package club.tianff.ulogin.rest;

import club.tianff.util.RestApiException;
import club.tianff.util.restapiresponse.Response;
import club.tianff.util.restapiresponse.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by tianfei on 2017/8/18.
 */
@ControllerAdvice(basePackages="club.tianff.ulogin.rest")
public class ApiHandler {

    private final static Logger logger = LoggerFactory.getLogger(ApiHandler.class);

    @ExceptionHandler
    @ResponseBody
    public Response handle(Exception e) {
        if (e instanceof RestApiException) {
            RestApiException sE = (RestApiException) e;
            return ResponseUtil.error(sE.getCode(), sE.getMessage());
        } else{
            logger.error("【系统异常】{}", e);
            return ResponseUtil.error(-1, "未知错误");
        }
    }

}
